unit uCompareProcessThread;

interface

uses
  System.Classes, System.SysUtils, System.Variants, System.Win.ComObj, Vcl.ComCtrls,
  Vcl.StdCtrls;

type
  TCompareProcess = class(TThread)
  private
    { Private declarations }
    ProgressBar: TProgressBar;
    StatusBar: TStatusBar;
    PBStyle: TProgressBarStyle;
    ProcessExcelFileBtn: TButton;
    UpdateBtn: TButton;
    StatusText: string;
    StartDate: TDateTime;
    EndDate: TDateTime;
    Predpr: Int64;
    FilePath: string;

    procedure SetProgressBarStyle;
    procedure SetStatusBarText;
    procedure ChangeControlAvailability;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean; StartDate, EndDate: TDateTime;
                       FilePath: string; Predpr: Int64;
                       ProgressBar: TProgressBar; StatusBar: TStatusBar;
                       ProcessExcelFileBtn, UpdateBtn: TButton); overload;
  end;

var
  CompareProcess: TCompareProcess;

implementation

uses uCompDM;

{ TCompareProcess }

procedure TCompareProcess.ChangeControlAvailability;
begin
  Self.ProcessExcelFileBtn.Enabled := not Self.ProcessExcelFileBtn.Enabled;
  Self.UpdateBtn.Enabled := not Self.UpdateBtn.Enabled;
end;

procedure TCompareProcess.SetStatusBarText;
begin
  StatusBar.SimpleText := StatusText;
end;

procedure TCompareProcess.SetProgressBarStyle;
begin
  ProgressBar.Style := PBStyle;
end;

constructor TCompareProcess.Create(CreateSuspended: Boolean; StartDate,
                                   EndDate: TDateTime; FilePath: string;
                                   predpr: Int64; ProgressBar: TProgressBar;
                                   StatusBar: TStatusBar; ProcessExcelFileBtn,
                                   UpdateBtn: TButton);
begin
  inherited Create(CreateSuspended);

  Self.ProgressBar := ProgressBar;
  Self.StatusBar := StatusBar;
  Self.FilePath := FilePath;
  Self.ProcessExcelFileBtn := ProcessExcelFileBtn;
  Self.UpdateBtn := UpdateBtn;

  Self.StartDate := StartDate;
  Self.EndDate := EndDate;
  Self.Predpr := Predpr;
end;

procedure TCompareProcess.Execute;
var ExcelApp, WorkSheet, FData: OLEVariant;
    i, Rows: Integer;
    PredprId: Int64;
begin
  ExcelApp := CreateOleObject('Excel.Application');
  ExcelApp.Workbooks.Open(Self.FilePath);

  Synchronize(ChangeControlAvailability);

  Self.PBStyle := pbstMarquee;
  Synchronize(SetProgressBarStyle);

  with CompDM do
  try
//    Synchronize(CompareMemTableEh.DisableControls);
//    Synchronize(CompareMemTableEh.EmptyTable);

    Self.StatusText := 'Загрузка файла камерального контроля...';
    Synchronize(SetStatusBarText);

    WorkSheet:= ExcelApp.Workbooks[1].WorkSheets[1];
    Rows:=WorkSheet.UsedRange.Rows.Count;
    FData:=WorkSheet.UsedRange.Value;

    Self.StatusText := 'Вставка записей в таблицу...';
    Synchronize(SetStatusBarText);

    for I := 3 to Rows do
//      if ((VarToDateTime(FData[I, 19]) >= Self.StartDate) and (VarToDateTime(FData[I, 19]) <= Self.EndDate)) or
//         ((VarToDateTime(FData[I, 39]) >= Self.StartDate) and (VarToDateTime(FData[I, 39]) <= Self.EndDate)) then
    begin
      if Length(Trim(VarToStr(FData[I, 3])))=0 then
        PredprId := StrToInt64(Trim(VarToStr(FData[I, 2]))+'0000')
      else
        PredprId := StrToInt64(Trim(VarToStr(FData[I, 2]))+Trim(VarToStr(FData[I, 3])));

      InsertExcelTemp(PredprId, VarToStr(FData[I, 4]), VarToStr(FData[I, 15]),
                      VarToStr(FData[I, 38]), 'П', VarToDateTime(FData[I, 19]),
                      VarToDateTime(FData[I, 39]), 5,
                      StrToCurr(UpdateDecimalSeparator(VarToStr(FData[I, 43]))),
                      Null, Null, Null, Null, Null);

//        if (Self.Predpr<>0) and (PredprId<>Self.Predpr) then Continue;

//        CompareMemTableEh.Insert;
//        CompareMemTableEhidPredpr.Value := PredprId;
//        CompareMemTableEhnamePredpr.Value := FData[I, 4];
//        CompareMemTableEhStatus.Value := 5;
//        CompareMemTableEhInvoiceFullName.Value := FData[I, 15];
//        CompareMemTableEhdateTrans.Value := FData[I, 19];
//        CompareMemTableEhdocNumber.Value := FData[I, 38];
//        CompareMemTableEhdocDate.Value := FData[I, 39];
//        CompareMemTableEhNDSExcel.Value := StrToCurr(UpdateDecimalSeparator(VarToStr(FData[I, 43])));
//        CompareMemTableEhTotalCostVatInvoice.Value := 0;
//        CompareMemTableEhTotalCostInvoice.Value := 0;
//        CompareMemTableEhSVPlace.Value := 'П';
//        CompareMemTableEh.Post;
    end;
  finally
    Self.StatusText := 'Выгрузка файла камерального контроля...';
    Synchronize(SetStatusBarText);

    ExcelApp.Quit;
    ExcelApp := Unassigned;
    WorkSheet := Unassigned;

//    Synchronize(CompareMemTableEh.EnableControls);
    CompareMemTableEh.Active := True;
    Synchronize(CompareMemTableEh.First);
  end;

  Self.StatusText := 'Обработка завершена';
  Synchronize(SetStatusBarText);

  Synchronize(ChangeControlAvailability);

  Self.PBStyle := pbstNormal;
  Synchronize(SetProgressBarStyle);
end;

end.
